/**
 * creates or updates the database for the ToDoList application
 * 
 * @author Nichlos Incardone
 * @version 1.0
 */

package com.BlackMesa.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseCreator extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "ToDoDatabase.db";

	/**
	 * The sql queries for all the tables
	 */
	private static final String USER_DATABASE_CREATE = "CREATE TABLE USERS (user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, name TEXT, email TEXT, pass TEXT)";
	private static final String CATEGORY_DATABASE_CREATE = "CREATE TABLE CATEGORIES (category_id INTEGER PRIMARY KEY AUTOINCREMENT, category TEXT NOT NULL, color INTEGER NOT NULL, user_id INTEGER NOT NULL)";
	private static final String TASK_DATABASE_CREATE = "CREATE TABLE TASKS (task_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, dueDate TEXT, detailedDescription TEXT NOT NULL, isComplete TEXT NOT NULL, location_id INTEGER, category_id INTEGER, user_id INTEGER, dueTime TEXT)";
	private static final String LOCATION_DATABASE_CREATE = "CREATE TABLE LOCATIONS (location_id INTEGER PRIMARY KEY AUTOINCREMENT, lat INTEGER, lon INTEGER, name TEXT, user_id INTEGER)";
	/**
	 * instantiates the database
	 * 
	 * @param context
	 */
	public DatabaseCreator(Context context) {
		super(context, DATABASE_NAME, null, 48);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(USER_DATABASE_CREATE);
		database.execSQL(CATEGORY_DATABASE_CREATE);
		database.execSQL(TASK_DATABASE_CREATE);
		database.execSQL(LOCATION_DATABASE_CREATE);
		

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DatabaseCreator.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS USERS");
		db.execSQL("DROP TABLE IF EXISTS CATEGORIES");
		db.execSQL("DROP TABLE IF EXISTS TASKS");
		db.execSQL("DROP TABLE IF EXISTS LOCATIONS");
		onCreate(db);
	}
}
